package handle

import (
	"fmt"
	"io"

	utilx "protocx/entity"

	"github.com/gogf/gf/v2/net/ghttp"
)

type Resp struct {
}

func (p Resp) InitRouter(group *ghttp.RouterGroup) {
	r := group.Group("/resp")
	r.GET("json", p.json())
	r.GET("jsonp", p.jsonp())
	r.GET("xml", p.xml())
	r.GET("binary", p.binary())
	r.GET("array", p.array())
	r.GET("raw", p.raw())
	r.GET("string", p.string())
	r.GET("object", p.object())
	r.GET("form-data", p.formdata())
	r.GET("form-ulencoded", p.formulencoded())
}

func (p Resp) Bind() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.Writef(
			"Get: form: %d, %s",
			r.GetQuery("age").Int(),
			r.GetQuery("name").String(),
		)
	}
}

func (p Resp) json() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		json, err := r.GetJson()
		if err != nil {
			utilx.RespHandle(400, "error", err)
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", json.Interface()))
	}
}
func (p Resp) jsonp() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		json, err := r.GetJson()
		if err != nil {
			utilx.RespHandle(400, "error", err)
			return
		}
		r.Response.WriteJsonP(utilx.RespHandle(200, "success", json.Interface()))
	}
}

func (p Resp) BodyJsonParse() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}

func (p Resp) raw() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		all, err := io.ReadAll(r.Body)
		if err != nil {
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", string(all)))
	}
}
func (p Resp) formdata() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		for k, v := range r.MultipartForm.File {
			fmt.Println(k, v)
		}
		for k, v := range r.MultipartForm.Value {
			fmt.Println(k, v)
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", r.Form))
	}
}
func (p Resp) formulencoded() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.WriteJson(utilx.RespHandle(200, "success", r.Form))
	}
}

func (p Resp) binary() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.Writer.ResponseWriter.Write([]byte{1, 2, 3})
	}
}
func (p Resp) object() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		var user utilx.User
		err := r.Parse(&user)
		if err != nil {
			r.Response.Write(utilx.RespHandle(200, "error", err))
			return
		}
		r.Response.WriteJson(utilx.RespHandle(200, "success", user))
	}
}
func (p Resp) xml() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.WriteXml(utilx.RespHandle(200, "success", nil))
	}
}
func (p Resp) array() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.WriteExit(utilx.RespHandle(200, "success", nil))
	}
}
func (p Resp) string() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.WriteString("string")
	}
}
func (p Resp) protobuf() ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		r.Response.WriteJsonP("string")
	}
}
